package com.wuze.cloud.controller;

import com.wuze.cloud.request.*;
import com.wuze.cloud.response.PreCheckResponse;
import com.wuze.cloud.response.SysFileResponse;
import com.wuze.cloud.result.Result;
import com.wuze.cloud.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: wuze
 * @description:
 * @date: 2023/02/18
 */
@RestController
public class SysFileController {

    public static final String PREFIX_PATH = "/sys/file/";
    @Autowired
    private SysFileService fileService;

    /**
     * 用户分页查询文件
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"findByPage/v1")
    public Result<SysFileResponse> findByPage(@RequestBody SysFileQuery query){
        return Result.success(fileService.findByPage(query));
    }

    /**
     * 管理员分页查询文件
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"findPageByAdmin/v1")
    public Result<SysFileResponse> findPageByAdmin(@RequestBody SysAdminFileQuery query){
        return Result.success(fileService.findPageByAdmin(query));
    }

    /**
     * 文件上传预检查
     * @return
     */
    @PostMapping(PREFIX_PATH+"preCheck/v1")
    public Result<PreCheckResponse> preCheck(@Validated @RequestBody SysFilePreCheckQuery query){
        return Result.success(fileService.preCheck(query));
    }

    /**
     * 文件分片上传
     * @param query
     * @return
     * @throws Exception
     */
    @PostMapping(PREFIX_PATH+"upload/v1")
    public Result<Boolean> upload(@RequestBody SysFileChunkQuery query) throws Exception {
        fileService.upload(query);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 文件批量下载
     *
     * @param query
     * @param response
     */
    @PostMapping(PREFIX_PATH + "batchDownload/v1")
    public void batchDownload(@RequestBody SysDownloadQuery query, HttpServletResponse response) {
        fileService.batchDownload(query, response);
    }

    /**
     * 用户文件批量删除
     *
     * @param query
     */
    @PostMapping(PREFIX_PATH + "batchDelete/v1")
    public Result<Boolean> batchDelete(@RequestBody SysDeleteFileQuery query) {
        fileService.batchdelete(query);
        return Result.success(Boolean.TRUE);
    }
    /**
     * 管理员文件批量删除
     *
     * @param querys
     */
    @PostMapping(PREFIX_PATH + "batchDeleteAdmin/v1")
    public Result<Boolean> batchDeleteAdmin(@RequestBody List<SysDeleteFileAdminQuery> querys) {
        fileService.batchdeleteAdmin(querys);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 修改文件
     *
     * @param query
     */
    @PostMapping(PREFIX_PATH + "update/v1")
    public Result<Boolean> update(@RequestBody SysUpdateFileQuery query) {
        fileService.update(query);
        return Result.success(Boolean.TRUE);
    }


    /**
     * 管理员文件批量恢复
     *
     * @param querys
     */
    @PostMapping(PREFIX_PATH + "batchRestoreAdmin/v1")
    public Result<Boolean> batchRestoreAdmin(@RequestBody List<SysAdminRestoreQuery> querys) {
        fileService.batchRestoreAdmin(querys);
        return Result.success(Boolean.TRUE);
    }
}
